package com.company.huisuo.biz.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.company.huisuo.R
import com.company.huisuo.base.BaseActivity
import com.company.huisuo.biz.adapter.KRoomViewPagerAdapter
import com.company.huisuo.biz.dialog.DanceBookingDialog
import com.company.huisuo.databinding.ActivityKroomBinding
import com.lxj.xpopup.XPopup

/**
 * 我的K房
 */
class KRoomActivity : BaseActivity<ActivityKroomBinding>() {

    private val mDanceBookingDialog by lazy {
        XPopup.Builder(this).asCustom(DanceBookingDialog(this))
    }

    companion object {
        private val EXTRA_TYPE = "extra_type"
        val TYPE_ARTIST = 0
        val TYPE_DANCE = 1
        val TYPE_AURA = 2

        fun start(activity: AppCompatActivity, type: Int = TYPE_ARTIST) {
            Intent(activity, KRoomActivity::class.java).apply {
                putExtra(EXTRA_TYPE, type)
                activity.startActivity(this)
            }
        }
    }

    override fun initView() {
        mBinding.viewpager.adapter = KRoomViewPagerAdapter(this)
        mBinding.viewpager.isUserInputEnabled = false
        mBinding.viewpager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                mBinding.tvSwitchArtist.isSelected = position == 0
                mBinding.tvSwitchDance.isSelected = position == 1
                mBinding.tvSwitchAura.isSelected = position == 2

                val param = mBinding.viewSwitchPoint.layoutParams as ConstraintLayout.LayoutParams
                when (position) {
                    0 -> {
                        param.startToStart = R.id.iv_switch_bg
                        param.endToEnd = ConstraintLayout.LayoutParams.UNSET
                    }
                    1 -> {
                        param.startToStart = R.id.iv_switch_bg
                        param.endToEnd = R.id.iv_switch_bg
                    }
                    2 -> {
                        param.startToStart = ConstraintLayout.LayoutParams.UNSET
                        param.endToEnd = R.id.iv_switch_bg
                    }
                }
                mBinding.viewSwitchPoint.layoutParams = param
            }
        })
        mBinding.tvSwitchArtist.isSelected = true
        mBinding.tvSwitchArtist.setOnClickListener {
            mBinding.viewpager.currentItem = 0
        }
        mBinding.tvSwitchDance.setOnClickListener {
            mBinding.viewpager.currentItem = 1
        }
        mBinding.tvSwitchAura.setOnClickListener {
            mBinding.viewpager.currentItem = 2
        }
        mBinding.ivBack.setOnClickListener {
            finish()
        }
        mBinding.ivRecord.setOnClickListener {
            mDanceBookingDialog.show()
        }
        mBinding.tvYexiao.setOnClickListener {
            MyOrderActivity.start(this)
        }
        mBinding.tvIntoGame.setOnClickListener {
            DanceGameActivity.start(this)
        }
    }

    override fun initData() {
        val type = intent.getIntExtra(EXTRA_TYPE, TYPE_ARTIST)
        mBinding.viewpager.currentItem = type
    }
}